// over-ride code in Firebug with our own functionality.
var FirebugLib = FBL = XPCOMUtils;

(function()
{
    /**
     * Creates a menuItem and adds it to the passed popup.
     * @param popup The popup to add the created menu item to.
     * @param item The item to create, in the form:
     *  {
     *      label:
     *      type:
     *      [checked: bool]
     *      [disabled: bool]
     *      [ image: class: ]
     *  }
     *
     * OR
     *  {
     *      menuItems: (array of the above object),
     *      label:
     *  }
     */
    FirebugLib.createMenuItem = function( popup, item )
    {
        try
        {
            if ( typeof(item) == "string" && item.indexOf("-") == 0 )
                return FirebugLib.createMenuSeparator(popup);
            if ( "menuItems" in item )
            {
                var items = item.menuItems;
                var menuPopup = FirebugLib.createMenu(popup, item.label);
                for ( var i = 0; i < items.length; ++i )
                {
                    FirebugLib.createMenuItem(menuPopup, items[i]);
                }
                return menuPopup;
            }
            var menuitem = popup.ownerDocument.createElement("menuitem");
            var label = item.nol10n ? item.label : this.$STR(item.label);
            menuitem.setAttribute("label", label);
            menuitem.setAttribute("type", item.type);
            if ( item.checked )
                menuitem.setAttribute("checked", "true");
            if ( item.disabled )
                menuitem.setAttribute("disabled", "true");
            if ( item.image )
            {
                menuitem.setAttribute("class", "menuitem-iconic");
                menuitem.setAttribute("image", item.image);
            }
            if ( item.command )
                menuitem.addEventListener("command", item.command, false);
            popup.appendChild(menuitem);
            return menuitem;
        }
        catch( ex )
        {
            Firebug.TestFire.util.tf_debug(ex);
        }

    };

})();



(function()
{
    /**
     * Creates a menuItem and adds it to the passed popup.
     * @param popup The popup to add the created menu item to.
     * @param item The item to create, in the form:
     *  {
     *      label:
     *      type:
     *      [checked: bool]
     *      [disabled: bool]
     *      [ image: class: ]
     *  }
     *
     * OR
     *  {
     *      menuItems: (array of the above object),
     *      label:
     *  }
     */
    FirebugLib.getElementXPath = function( element, forIE )
    {
        try
        {
            if ( element && element.id )
            {
                return '//*[@id="' + element.id + '"]';
            }
            else
            {
                return FBL.getElementTreeXPath(element, forIE);
            }
        }
        catch( ex )
        {
            Firebug.TestFire.util.tf_debug(ex);
        }

    };

    FirebugLib.getElementTreeXPath = function( element, forIE )
    {
        var paths = [];

        for ( ; element && element.nodeType == 1; element = element.parentNode )
        {
            var index = 0;
            for ( var sibling = element.previousSibling; sibling; sibling = sibling.previousSibling )
            {
                if ( sibling.localName == element.localName )
                    ++index;
            }

            var tagName = forIE ? element.localName : element.localName.toLowerCase();
            var pathIndex = (index ? "[" + (index + 1) + "]" : "");
            paths.splice(0, 0, tagName + pathIndex);
        }

        return paths.length ? "/" + paths.join("/") : null;
    };



})();

(function()
{
    with ( XPCOMUtils )
    {

        top.FirebugChrome.contextIsShowing = 0;
        top.FirebugChrome.onContextHiding = function( event )
        {
            this.contextIsShowing--;
        };

        top.FirebugChrome.onContextShowing = function( event )
        {
            try
            {
                var panelBar = $("fbPanelBar1");
                if ( !panelBar.selectedPanel )
                    return false;
                if ( this.contextIsShowing++ )
                {
                    return;
                }
                var popup = $("fbContextMenu");
                var target = document.popupNode;
                var panel = target ? Firebug.getElementPanel(target) : null;
                FBL.eraseNode(popup);
                if ( !this.contextMenuObject && !$("cmd_copy").getAttribute("disabled") )
                {
                    var menuitem = FBL.createMenuItem(popup, {label: "Copy"});
                    menuitem.setAttribute("command", "cmd_copy");
                }
                var object;
                if ( this.contextMenuObject )
                    object = this.contextMenuObject;
                else if ( target && target.ownerDocument == document )
                    object = Firebug.getRepObject(target);
                else if ( target )
                    object = panel.getPopupObject(target);
                this.contextMenuObject = null;
                var rep = Firebug.getRep(object);
                var realObject = rep ? rep.getRealObject(object, FirebugContext) : null;
                var realRep = realObject ? Firebug.getRep(realObject) : null;
                if ( realObject && realRep )
                {
                    // 1. Add the custom menu items from the realRep
                    var menu = realRep.getContextMenuItems(realObject, target, FirebugContext);
                    if ( menu )
                    {
                        for ( var i = 0; i < menu.length; ++i )
                            FBL.createMenuItem(popup, menu[i]);
                    }
                }
                if ( object && rep && rep != realRep )
                {
                    // 1. Add the custom menu items from the original rep
                    var items = rep.getContextMenuItems(object, target, FirebugContext);
                    if ( items )
                    {
                        for ( var i = 0; i < items.length; ++i )
                            FBL.createMenuItem(popup, items[i]);
                    }
                }
                if ( panel )
                {
                    var items = panel.getContextMenuItems(realObject, target);
                    if ( items )
                    {
                        for ( var i = 0; i < items.length; ++i )
                            FBL.createMenuItem(popup, items[i]);
                    }
                }
                if ( realObject && rep && rep.inspectable )
                {
                    var separator = null;

                    var items = this.getInspectMenuItems(realObject);
                    for ( var i = 0; i < items.length; ++i )
                    {
                        if ( popup.firstChild && !separator )
                            separator = FBL.createMenuSeparator(popup);

                        FBL.createMenuItem(popup, items[i]);
                    }
                }
                if ( !popup.firstChild )
                    return false;
            }
            catch( ex )
            {
                Firebug.TestFire.util.tf_debug(ex);
            }

























        // 1. Add the custom menu items from the panel


            // 2. Add the inspect menu items

        }

    }
})();

(function()
{
    with ( FBL )
    {
        top.Firebug.getRep = function( object )
        {
            var type = typeof(object);
            var retRep;
            for ( var i = 0; i < top.Firebug.reps.length; ++i )
            {
                var rep = top.Firebug.reps[i];
                try
                {
                    if ( rep.supportsObject(object, type) )
                    {
                        // takes the first rep if specificness is set to default (0)
                        if ( !retRep || retRep.specificness < rep.specificness )
                        {
                            retRep = rep;
                        }
                    }
                }
                catch ( exc )
                {}
            }
            if ( !retRep )
                retRep = top.Firebug.defaultRep;
            return retRep;
        }

        Firebug.Rep.specificness = 0;
    }
})();